package com.wanmait.employment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.employment.entity.*;
import com.wanmait.employment.mapper.CompanyMapper;
import com.wanmait.employment.mapper.PublisherMapper;
import com.wanmait.employment.service.PublisherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class PublisherServiceImpl implements PublisherService {
     @Resource
     private PublisherMapper publisherMapper;
     @Resource
     private CompanyMapper companyMapper;

    @Override
    public PageInfo<Publisher> list(String publishername, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Publisher> queryWrapper=new QueryWrapper<>();
        if(publishername.equals("在职")){
            queryWrapper.eq("is_depart",1);
        }else if(publishername.equals("离职")) {
            queryWrapper.eq("is_depart",0);
        }else {
            queryWrapper.like("publishername", publishername).or()
                    .inSql("company_id", "SELECT id FROM Company WHERE company_name like '%" + publishername + "%'");
        }
        List<Publisher> publisherList =publisherMapper.selectList(queryWrapper);
        PageInfo<Publisher> publisherPageInfo =new PageInfo<>(publisherList);

        publisherPageInfo.getList().forEach(publisher -> {
            QueryWrapper<Company> companyTypeQueryWrapper = new QueryWrapper<>();
            companyTypeQueryWrapper.eq("id",publisher.getCompanyId());
            Company company=companyMapper.selectOne(companyTypeQueryWrapper);
            if(publisher.getSex().equals("男")){
                publisher.setStandbyName(publisher.getName().substring(0,1)+"先生");
            }
            else{
                publisher.setStandbyName(publisher.getName().substring(0,1)+"女士");

            }


            publisher.setCompany(company);
             });


        return publisherPageInfo;
    }

    @Override
    public Integer resetpasswords(Integer id, String password) {
        return publisherMapper.resetpasswords(id,password);
    }

    @Override
    public Integer resetpic(Integer id, String resetpic) {

        return publisherMapper.resetpic(id,resetpic);
    }

    @Override
    public Integer update(Publisher publisher) {
        return publisherMapper.updateById(publisher);
    }

    @Override
    public Integer delete(Integer id) {
        return publisherMapper.deleteById(id);
    }

    //前台查公司的人事-----宋立新
    @Override
    public List<Publisher> searchList(Integer companyId) {
        QueryWrapper<Publisher> publisherQueryWrapper = new QueryWrapper<>();
        publisherQueryWrapper.eq(" company_id", companyId);
        List<Publisher> publisherList = publisherMapper.selectList(publisherQueryWrapper);
        Collections.shuffle(publisherList); // 打乱列表顺序
        int limit = Math.min(publisherList.size(), 3); // 确定最终选取的元素数量
        publisherList = publisherList.subList(0, limit); // 截取前 limit 个元素
        return publisherList;
    }

    @Override
    public Integer register(Publisher publisher) {
        return publisherMapper.insert(publisher);
    }

    @Override
    public Publisher login(Publisher publisher) {
        if(StringUtils.isBlank(publisher.getTel())|| StringUtils.isBlank(publisher.getPassword())){
            return null;
        }
        LambdaQueryWrapper<Publisher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Publisher::getTel,publisher.getTel());
        queryWrapper.eq(Publisher::getPassword,publisher.getPassword());
        return publisherMapper.selectOne(queryWrapper);
    }

    @Override
    public void saveOrUpdate(Publisher publisher) {
        LambdaQueryWrapper<Publisher> publisherLambdaQueryWrapper=new LambdaQueryWrapper<>();
        publisherLambdaQueryWrapper.eq(Publisher::getId,publisher.getId());
        Publisher p=publisherMapper.selectOne(publisherLambdaQueryWrapper);
       String name= p.getName().substring(0,1);
        if(p.getSex().equals("男")){
            p.setStandbyName(name+"先生");
        }else{
            p.setStandbyName(name+"女士");
        }
        if(p!=null){
            publisherMapper.updateById(publisher);
        }else{
            publisherMapper.insert(publisher);
        }

    }
}
